package wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public  class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    Text k = new Text();
    private final static IntWritable one = new IntWritable(1);
    private String pattern = "[^//w]"; // 正则表达式，代表不是0-9, a-z, A-Z的所有其它字符,其中还有下划线
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {


        // 1 获取一行
        String line = value.toString().toLowerCase(); // 全部转为小写字母
        line.replaceAll(pattern, " "); // 将非0-9, a-z, A-Z的字符替换为空格

        // 2 切割
        String[] words = line.split(" ");

        // 3 输出
        for (String word : words) {

            k.set(word);
            context.write(k, one);

        }
    }
}
